package com.database.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Named;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.database.bean.Game;
import com.database.dao.GameDao;

@Named("gameDao")
@Component
public class GameDaoImpl extends BaseDaoImpl<Game> implements GameDao {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private SessionFactory sessionFactory;
	

	
	public GameDaoImpl() {
		super(Game.class);
	}
	
	/**
	 * Method to read the top selling companyproducts with their information per game
	 * @param game
	 * @return
	 */
	public HashMap<String,String> getGameDropdown() {
		HashMap<String,String> result=new HashMap<String,String>();
		Session session = sessionFactory.openSession();
		//Transaction tx = null;
		try {
			//tx = session.beginTransaction();
			String sql = "select name,id from game";
			SQLQuery query = session.createSQLQuery(sql);
			query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
			@SuppressWarnings("unchecked")
			List<Object> data = query.list();

			for (Object object : data) {
				@SuppressWarnings({ "unchecked", "rawtypes" })
				Map<Object,Object> row = (Map) object;
				result.put((String)row.get("name"), row.get("id").toString());
			}
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		
		return result;
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		logger.debug("Set Autowired SessionFactory: {}",sessionFactory);
		this.sessionFactory = sessionFactory;
	}

	
}
